//
//  JYInfoInputView.m
//  MYCCBRegisterSubAccount
//
//  Created by 应明顺 on 2018/7/5.
//  Copyright © 2018年 JackYing. All rights reserved.
//


#import "JYBaseInputView.h"
#import "JYVerifyRegEx.h"

@interface JYBaseInputView () {
    UITextField *_textField;
}

@end

@implementation JYBaseInputView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _textField = [[UITextField alloc] initWithFrame:frame];
        _textField.textAlignment = NSTextAlignmentLeft;
        _textField.delegate = self;
        _textField.font = [UIFont systemFontOfSize:15];
        [self addSubview:_textField];
                
        _textField.rightViewMode = UITextFieldViewModeAlways;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _textField.frame = CGRectMake(self.textEdgeInsets.left, self.textEdgeInsets.top,
                                  CGRectGetWidth(self.bounds) - self.textEdgeInsets.left - self.textEdgeInsets.right,
                                  CGRectGetHeight(self.bounds) - self.textEdgeInsets.top - self.textEdgeInsets.bottom);
}


#pragma mark - 对UITextField进行配置
- (void)setPlaceholder:(NSString *)placeholder {
    if (![_placeholder isEqualToString:placeholder]) {
        _placeholder = placeholder;
    }
    _textField.placeholder = placeholder;
}

- (void)setText:(NSString *)text {
    if (![_text isEqualToString:text]) {
        _text = text;
    }
    _textField.text = text;
}

- (void)setKeyboardType:(UIKeyboardType)keyboardType {
    if (_keyboardType != keyboardType) {
        _keyboardType = keyboardType;
    }
    _textField.keyboardType = keyboardType;
}

- (void)setClearButtonMode:(UITextFieldViewMode)clearButtonMode {
    _clearButtonMode = clearButtonMode;
    _textField.clearButtonMode = _clearButtonMode;
}

- (void)setClearsOnBeginEditing:(BOOL)clearsOnBeginEditing {
    _clearsOnBeginEditing = clearsOnBeginEditing;
    _textField.clearsOnBeginEditing = clearsOnBeginEditing;
}

- (void)setRightView:(UIView *)rightView {
    if (_rightView != rightView) {
        _rightView = rightView;
    }
    _textField.rightView = rightView;
}

- (void)setRegEx:(NSString *)regEx {
    if (regEx != _regEx) {
        _regEx = regEx;
    }
}

- (void)setContent:(id)content {
    if ([content isKindOfClass:[NSString class]]) {
        _textField.text = content;
    }
}

- (void)setTextEdgeInsets:(UIEdgeInsets)textEdgeInsets {
    _textEdgeInsets =textEdgeInsets;
    if (_textField) {
        CGRect frame = _textField.frame;
        frame.origin.x += textEdgeInsets.left;
        frame.origin.y += textEdgeInsets.top;
        frame.size.width -= (textEdgeInsets.left + textEdgeInsets.right);
        frame.size.height -= (textEdgeInsets.top + textEdgeInsets.bottom);
        _textField.frame = frame;
    }
}

#pragma mark - <UITextFieldDelegate>
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    _textField = textField;
    if ([self.delegate respondsToSelector:@selector(inputViewDidBeginInputView:)]) {
        [self.delegate inputViewDidBeginInputView:self];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    // 删除
    if ([@"" isEqualToString:string]) {
        return YES;
    }
    if (textField != _textField) {
        return NO;
    }
    
    NSString *tmp = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    return self.regEx ? [JYVerifyRegEx verifyInputString:tmp regEx:self.regEx] : YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (textField == _textField) {
        if ([self.delegate respondsToSelector:@selector(inputView:didFinishInput:)]) {
            [self.delegate inputView:self didFinishInput:textField.text];
        }
    }
}


@end
